/**
 * 
 */
package com.yi.backend.model.api.impl;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import com.mongodb.DBObject;
import com.yi.backend.db.api.IMongoDBHelperApi;
import com.yi.backend.db.api.impl.MongoDBConstants;
import com.yi.backend.model.BusinessModel;
import com.yi.backend.model.CommonModel;
import com.yi.backend.model.api.IBusinessModelApi;



/**
 * @author long
 *
 */
public class MongoBussinessModelApi extends MongoCommonModelApi implements IBusinessModelApi{
	
	private IMongoDBHelperApi mongoDBHelperApi;
	
	public IMongoDBHelperApi getMongoDBHelperApi() {
		return mongoDBHelperApi;
	}

	public void setMongoDBHelperApi(IMongoDBHelperApi mongoDBHelperApi) {
		this.mongoDBHelperApi = mongoDBHelperApi;
	}
	
	private DBObject convertBusinessModelToEntity(BusinessModel model){
		DBObject entity = convertCommonModelToEntity(model);
		if(model.fields != null && model.fields.size() > 0){
			entity.put(MongoDBConstants.fieldsCol, model.fields);	
		}
		return entity;
	}
	
	private BusinessModel convertEntityToBusinessModel(DBObject entity){
		BusinessModel businessModel = new BusinessModel(convertEntityToCommonModel(entity));
		if(entity.get(MongoDBConstants.fieldsCol) != null) 
			businessModel.fields = ((Map<String,Object>)entity.get(MongoDBConstants.fieldsCol));
		return businessModel;
	}
	@Override
	public String create(String schemaName, BusinessModel model) {
		// TODO Auto-generated method stub
		return mongoDBHelperApi.create(schemaName, convertBusinessModelToEntity(model));
	}

	@Override
	public void update(String schemaName, BusinessModel model) {
		// TODO Auto-generated method stub
		mongoDBHelperApi.update(schemaName, convertBusinessModelToEntity(model));
	}

	@Override
	public BusinessModel viewById(String schemaName, String modelId) {
		// TODO Auto-generated method stub
		return convertEntityToBusinessModel(mongoDBHelperApi.viewById(schemaName, modelId));
	}

	@Override
	public BusinessModel viewByName(String schemaName, String modelName) {
		// TODO Auto-generated method stub
		return convertEntityToBusinessModel(mongoDBHelperApi.viewByName(schemaName, modelName));
	}

	@Override
	public void removeById(String schemaName, String modelId) {
		// TODO Auto-generated method stub
		mongoDBHelperApi.removeById(schemaName, modelId);
	}

	@Override
	public List<BusinessModel> list(String schemaName, int pageNumber) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public void initialRoles() {
		// TODO Auto-generated method stub
		BusinessModel adminRole = new BusinessModel();
		adminRole.name = "admins";
		adminRole.desc = "the group for administrators";
		Map<String,Object> fields = new LinkedHashMap<String,Object>();
		Map<String,String> permissions = new LinkedHashMap<String,String>();
		permissions.put("*", "*");
		fields.put("permissions", permissions);
		adminRole.fields = fields;
		create(roleSchemaName, adminRole);
	}

	@Override
	public void initialUsers() {
		// TODO Auto-generated method stub
		BusinessModel adminUser = new BusinessModel();
		adminUser.name = "admin";
		adminUser.desc = "the people who is administrator";
		Map<String,Object> userRoles = new LinkedHashMap<String,Object>();
		List<String> roleNames = new ArrayList<String>();
		roleNames.add("admins");
		userRoles.put("roleNames", roleNames);
		adminUser.fields = userRoles;
		create(userSchemaName, adminUser);
		
	}
}
